package com.dao;


import com.domain.Message;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Repository
public class MessageDAOImpl implements MessageDAO {
    @Resource
    private SessionFactory sessionFactory;

    @Transactional
    public void addMessage(Message message) {
        sessionFactory.getCurrentSession().save(message);
    }

    @Transactional
    public List<Message> getMessages() {
        Session currentSession = sessionFactory.getCurrentSession();
        Criteria criteria = currentSession.createCriteria(Message.class);
        criteria.add(Restrictions.ge("time", System.currentTimeMillis() - 120000L));
        return criteria.list();
    }
}
